package com.alibaba.citrus.cr.b2b.trade.order.utils;

import com.alibaba.citrus.cr.b2b.trade.order.sdo.OrderSDO;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class TradeMessageUtil {
    private static Log log = LogFactory.getLog(TradeMessageUtil.class);
    /**
     * 将message转换为OrderSDO
     * @param body
     * @return
     */
    public static OrderSDO convertB2BTradeOrderMsg(byte[] body) {
        String messageBody = new String(body, StandardCharsets.UTF_8);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

        try {
            return (OrderSDO)objectMapper.readValue(messageBody, OrderSDO.class);
        } catch (IOException var4) {
            log.error("convert bytes to OrderSDO exception!", var4);
            return null;
        }
    }
}
